// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deskId: '',
    deskCaption: '',
    orderMenuList: [],
    lineNo:'',
    menuId:'',
    //数量
    menuCount:'',
    //符号
    fh:'',
    //共计金额
    sun:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.deskId)
    console.log(options.deskCaption)
    this.setData({
      deskId: options.deskId,
      deskCaption: options.deskCaption,
    })
    wx.request({
      url: 'http://www.szyzit.com:8080/diancan/app/modules/preOrder/template/PreOrderBodyTpl.html',
      method: 'GET'
    })
    this.getOrder()
  },
  //返回上一页
  toback() {
    wx.navigateBack({
      delta: 1
    })
  },
  //获取菜单
  getOrder() {
    wx.request({
      url: 'http://www.szyzit.com:8080/CateringSystem1.0/recruitment/catering/qryOrderMenuList.do?method=login',
      method: 'POST',
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        deskId: this.data.deskId,
        status: '0,1,2,3,4,5',
        listAll: 0
      },
      success: (res) => {
        let num1=0;
        res.data.responseBody.orderMenuList.map(item=>{
          num1=num1+item.promotionPrice
          return num1
        })
        this.setData({
          orderMenuList: res.data.responseBody.orderMenuList,
          sun:num1
        })
      },
      fail: (res) => {
        wx.showToast({
          title: '请求失败',
        })
      }
    })
  },
  //点击加减
  checkCount(e){
    this.setData({
      fh:e.currentTarget.dataset.fh,
      menuCount:e.currentTarget.dataset.menucount||0,
      lineNo:e.currentTarget.dataset.lineno,
      menuId:e.currentTarget.dataset.menuid,
    })
  },
  //子组件传过来的值
  thismyevent(e){
    console.log('选择的数字',e.detail)
    console.log('linenol',e.currentTarget)
    this.setData({
      js:e.detail
    })
    //判断加减
    if(this.data.fh=='-'){
      //减
      this.setData({
        menuCount:this.data.menuCount*1-e.detail*1
      })
      wx.request({
        url: 'http://www.szyzit.com:8080/CateringSystem1.0/recruitment/catering/subAccumlateMenu.do?method=login',
        method:'post',
        header:{'Content-type': 'application/x-www-form-urlencoded'},
        data:{
          lineNo:this.data.lineNo,
          menuCount:this.data.menuCount,
          accumlateFlag:1
        },
        success:(res)=>{
          this.setData({
            menuCount:''
          })
          this.getOrder()
        },
        fail:(res)=>{
          wx.showToast({
            title: '请求失败',
          })
        }
      })
    }else{
      this.setData({
        menuCount:this.data.menuCount*1+e.detail*1
      })
      wx.request({
        url: 'http://www.szyzit.com:8080/CateringSystem1.0/recruitment/catering/accumlateMenu.do?method=login',
        method:'post',
        header:{'Content-type': 'application/x-www-form-urlencoded'},
        data:{
          menuId:this.data.menuId,
          deskId:this.data.deskId,
          deskCaption:this.data.deskCaption,
          lineNo:this.data.lineNo,
          accumlateFlag: 1,
          menuCount:this.data.menuCount,
        },
        success:(res)=>{
          this.setData({
            menuCount:''
          })
          this.getOrder()
        },
        fail:(res)=>{
          wx.showToast({
            title: '请求失败',
          })
        }
      })
    }
  },
  //加菜
  addcai(){
    wx.navigateBack({
      delta: 1
    })
  },
  //清空
  delcai(){
    wx.showModal({
      title: '提示',
      content: '是否清空菜单',
      success:(res)=> {
        if (res.confirm) {
          wx.request({
            url: 'http://www.szyzit.com:8080/CateringSystem1.0/recruitment/catering/emptyOrderMenuByDeskId.do?method=login',
            method:'POST',
            header:{'Content-type': 'application/x-www-form-urlencoded'},
            data:{
              deskId:this.data.deskId
            },
            success:(res)=>{
              this.getOrder()
            },
            fail:(res)=>{
              wx.showToast({
                title: '请求失败',
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  
    
  },
  //快速下单
  ksxiadan(){
    wx.navigateTo({
      url: '/pages/specia/specia?deskId='+this.data.deskId+'&deskCaption='+this.data.deskCaption,
    })
  }
})